package com.deparse.dpframe.ui.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import java.util.List;

/**
 * Created by hsy on 2016/2/2.
 */
public class DPBaseApplication extends Application {
    private Toast toast = null;

    public String getProcessName(Context cxt) {
        try {
            int myPid = android.os.Process.myPid();
            ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
            if (runningApps == null) {
                return null;
            }
            for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
                if (procInfo.pid == myPid) {
                    return procInfo.processName;
                }
            }
        } catch (Exception e) {
            //
        }
        return null;
    }

    /**
     * showToast
     *
     * @param resId
     * @param dependency
     */
    public void showToast(int resId, Boolean... dependency) {
        showToast(getString(resId), dependency);
    }

    /**
     * showToast
     *
     * @param msg
     * @param dependency
     */
    public void showToast(String msg, Boolean... dependency) {
        if (null != msg && 0 < dependency.length && true == dependency[0]) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
        if (null == toast) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }
}
